﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 20.03.2008
 * Zeit: 11:01
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */
using System;
using System.Collections.Generic;
using System.Xml;
using System.Drawing;

namespace Gamadus.Designer
{
	public class Room : Resource
	{
		List<Instance> Instances;
		public Room(int id):base(id)
		{
			this.type = ResourceType.Room;
			Settings["width"] = 640;
			Settings["height"] = 480;
			Settings["caption"] = "";
			Settings["speed"] = 30;
			Settings["persistent"] = false;
			Settings["bgcolor"] = Color.Gray;
			Settings["snapx"] = 16;
			Settings["snapy"] = 16;
			Instances = new List<Instance>();
		}
		
		public List<Instance> CloneInstanceList()
		{
			List<Instance> list = new List<Instance>();
			foreach(Instance inst in Instances)
			{
				list.Add(inst.Clone());
			}
			return list;
		}
		
		public void WriteInstanceList(List<Instance> list)
		{
			Instances = list;
		}
		
		protected override IEditWindow showEditForm()
		{
			return new EditRoomForm(this);
		}
		
		public override void WriteXml(XmlTextWriter xml)
		{
			xml.WriteStartElement("Instances");
			foreach(Instance inst in Instances)
			{
				xml.WriteStartElement("Instance");
				xml.WriteAttributeString("ObjectID", inst.ObjectID.ToString());
				xml.WriteAttributeString("X", inst.Position.X.ToString());
				xml.WriteAttributeString("Y", inst.Position.Y.ToString());
				xml.WriteEndElement();
			}
			xml.WriteEndElement();
		}
		
		public override void ReadXml(XmlElement element)
		{
			if(element.GetElementsByTagName("Instances").Count == 0)
				return;
			
			XmlElement instanceChild = (XmlElement)element.GetElementsByTagName("Instances")[0];
			foreach(XmlElement child in instanceChild.ChildNodes)
			{
				if(!child.HasAttribute("ObjectID") || !child.HasAttribute("X") || !child.HasAttribute("Y"))
					continue;
				
				int id = int.Parse(child.Attributes["ObjectID"].InnerText);
				int x = int.Parse(child.Attributes["X"].InnerText);
				int y = int.Parse(child.Attributes["Y"].InnerText);
				Instances.Add(new Instance(id, new Point(x,y)));
			}
		}
	}
}